package com.light.mytext.views.pics;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.ViewConfiguration;


/**
 * 加了手势伸缩的图片
 */
public class ImageViewTouch extends ImageViewTouchBase {
    static final float SCROLL_DELTA_THRESHOLD = 1.0f;
    private static final String TAG = "ImageViewTouch";
    //检测两个手指在屏幕上做缩放的手势
    protected ScaleGestureDetector mScaleDetector;
    //
    protected GestureDetector mGestureDetector;
    //触发移动事件的最小距离
    protected int mTouchSlop;
    //
    protected float mScaleFactor;
    //
    protected int mDoubleTapDirection;
    //
    protected OnGestureListener mGestureListener;
    //
    protected OnScaleGestureListener mScaleListener;
    //
    protected boolean mDoubleTapEnabled = true;
    //
    protected boolean mScaleEnabled = true;
    //
    protected boolean mScrollEnabled = true;
    //
    private OnImageViewTouchDoubleTapListener mDoubleTapListener;
    //
    private OnImageViewTouchSingleTapListener mSingleTapListener;
    //
    private OnImageFlingListener mFlingListener;

    // 属性变量
    public float translationX; // 移动X
    public float translationY; // 移动Y
    public float scale = 1; // 伸缩比例
    public float rotation; // 旋转角度

    // 移动过程中临时变量
    private float actionX;
    private float actionY;
    private float spacing;
    private float degree;
    private int moveType; // 0=未选择，1=拖动，2=缩放

    public ImageViewTouch(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void init() {
        super.init();
        mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
        mGestureListener = getGestureListener();
        mScaleListener = getScaleListener();
        mScaleDetector = new ScaleGestureDetector(getContext(), mScaleListener);
        mGestureDetector = new GestureDetector(getContext(), mGestureListener,
                null, true);

        mDoubleTapDirection = 1;
//        setinit();
    }

    public void setDoubleTapListener(OnImageViewTouchDoubleTapListener listener) {
        mDoubleTapListener = listener;
    }

    public void setSingleTapListener(OnImageViewTouchSingleTapListener listener) {
        mSingleTapListener = listener;
    }

    public void setFlingListener(OnImageFlingListener listener) {
        mFlingListener = listener;
    }

    public void setDoubleTapEnabled(boolean value) {
        mDoubleTapEnabled = value;
    }

    public void setScaleEnabled(boolean value) {
        mScaleEnabled = value;
        setDoubleTapEnabled(value);
    }

    public void setScrollEnabled(boolean value) {
        mScrollEnabled = value;
    }

    public boolean getDoubleTapEnabled() {
        return mDoubleTapEnabled;
    }

    protected OnGestureListener getGestureListener() {
        return new GestureListener();
    }

    protected OnScaleGestureListener getScaleListener() {
        return new ScaleListener();
    }

    @Override
    protected void _setImageDrawable(final Drawable drawable,
                                     final Matrix initial_matrix, float min_zoom, float max_zoom) {
        super._setImageDrawable(drawable, initial_matrix, min_zoom, max_zoom);
        mScaleFactor = getMaxScale() / 3;
    }

    /*public void setinit(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(frameColor);
        mPaint.setStrokeWidth(frameWidth);
        mPaint.setStyle(Paint.Style.STROKE);

        if(controlDrawable == null){
            controlDrawable = getContext().getResources().getDrawable(R.drawable.update);
        }

        mDrawableWidth = controlDrawable.getIntrinsicWidth();
        mDrawableHeight = controlDrawable.getIntrinsicHeight();

    }*/

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        mScaleDetector.onTouchEvent(event);
//
//        if (!mScaleDetector.isInProgress()) {
//            mGestureDetector.onTouchEvent(event);
//        }

        rotateDrag(event);

//        int action = event.getAction();
//        switch (action & MotionEvent.ACTION_MASK) {
//            case MotionEvent.ACTION_UP:
//                if (getScale() < getMinScale()) {
//                    zoomTo(getMinScale(), 500);
//                }
//                break;
//
//        }
        return true;
    }

//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        drawBox(canvas);
//    }

    private void drawBox(Canvas canvas) {
//        mHelpBoxRect.set(mCenterRect);
//        RectUtil.scaleRect(mHelpBoxRect, scale);
//
//        canvas.save();
//        canvas.scale(scale, scale, mHelpBoxRect.centerX(), mHelpBoxRect.centerY());
//        canvas.rotate(scale, mHelpBoxRect.centerX(), mHelpBoxRect.centerY());
//        //draw x and rotate button
//        int offsetValue = ((int) mDeleteDstRect.width()) >> 1;
//        Log.e(TAG, "drawBox: "+offsetValue );
//        mDeleteDstRect.offsetTo(mHelpBoxRect.left - offsetValue, mHelpBoxRect.top - offsetValue);
//        mRotateDstRect.offsetTo(mHelpBoxRect.right - offsetValue, mHelpBoxRect.top - offsetValue);
//        mZoomDstRect.offsetTo(mHelpBoxRect.right - offsetValue, mHelpBoxRect.bottom - offsetValue);
//
//        RectUtil.rotateRect(mDeleteDstRect, mHelpBoxRect.centerX(),
//                mHelpBoxRect.centerY(), scale);
//        RectUtil.rotateRect(mRotateDstRect, mHelpBoxRect.centerX(),
//                mHelpBoxRect.centerY(), scale);

//        if (!isShowHelpBox) {
//            return;
//        }
//
//        canvas.save();
////        canvas.rotate(scale, mHelpBoxRect.centerX(), mHelpBoxRect.centerY());
//        canvas.restore();
//
//        canvas.drawBitmap(mDeleteBitmap, mDeleteRect, mDeleteDstRect, null);
//        canvas.drawBitmap(mRotateBitmap, mRotateRect, mRotateDstRect, null);
//        canvas.drawBitmap(mZoomBitmap, mZoomRect, mZoomDstRect, null);

        //debug
//        canvas.drawRect(mRotateDstRect, debugPaint);
//        canvas.drawRect(mDeleteDstRect, debugPaint);
//        canvas.drawRect(mHelpBoxRect, debugPaint);
    }


    /**
     * 处理拖动事件
     *
     * @param event
     */
    protected void rotateDrag(MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
//                isShowHelpBox = true;
                moveType = 1;// 0=未选择，1=拖动，2=缩放
//                translationX = translationX + event.getRawX() - actionX;
//                translationY = translationY + event.getRawY() - actionY;
//                setTranslationX(translationX);
//                setTranslationY(translationY);
                actionX = event.getRawX();
                actionY = event.getRawY();
//                mPreMovePointF.set(event.getX() + mViewPaddingLeft, event.getY() + mViewPaddingTop);
//
//                mStatus = JudgeStatus(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
//                isShowHelpBox = true;
                moveType = 2;// 0=未选择，1=拖动，2=缩放
//                scale = scale * getSpacing(event) / spacing;
//                setScaleX(scale);
//                setScaleY(scale);
//                rotation = rotation + getDegree(event) - degree;
//                if (rotation > 360) {
//                    rotation = rotation - 360;
//                }
//                if (rotation < -360) {
//                    rotation = rotation + 360;
//                }
//                setRotation(rotation);
                spacing = getSpacing(event);
                degree = getDegree(event);
                break;
            case MotionEvent.ACTION_MOVE:
//                isShowHelpBox = true;
                if (moveType == 1) {// 0=未选择，1=拖动，2=缩放
                    translationX = translationX + event.getRawX() - actionX;
                    translationY = translationY + event.getRawY() - actionY;
                    setTranslationX(translationX);
                    setTranslationY(translationY);
                    actionX = event.getRawX();
                    actionY = event.getRawY();
                } else if (moveType == 2) {// 0=未选择，1=拖动，2=缩放
                    scale = scale * getSpacing(event) / spacing;
                    setScaleX(scale);
                    setScaleY(scale);
                    rotation = rotation + getDegree(event) - degree;
                    if (rotation > 360) {
                        rotation = rotation - 360;
                    }
                    if (rotation < -360) {
                        rotation = rotation + 360;
                    }
                    setRotation(rotation);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                moveType = 0;// 0=未选择，1=拖动，2=缩放
//                isShowHelpBox = false;
                break;
        }

    }

    // 触碰两点间距离
    private float getSpacing(MotionEvent event) {
        //通过三角函数得到两点间的距离
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    // 取旋转角度
    private float getDegree(MotionEvent event) {
        //得到两个手指间的旋转角度
        double delta_x = event.getX(0) - event.getX(1);
        double delta_y = event.getY(0) - event.getY(1);
        double radians = Math.atan2(delta_y, delta_x);
        return (float) Math.toDegrees(radians);
    }


    @Override
    protected void onZoomAnimationCompleted(float scale) {

        if (LOG_ENABLED) {
            Log.d(LOG_TAG, "onZoomAnimationCompleted. scale: " + scale
                    + ", minZoom: " + getMinScale());
        }

        if (scale < getMinScale()) {
            zoomTo(getMinScale(), 50);
        }
    }

    // protected float onDoubleTapPost(float scale, float maxZoom) {
    // if (mDoubleTapDirection == 1) {
    // mDoubleTapDirection = -1;
    // return maxZoom;
    // } else {
    // mDoubleTapDirection = 1;
    // return 1f;
    // }
    // }

    protected float onDoubleTapPost(float scale, float maxZoom) {
        if (mDoubleTapDirection == 1) {
            if ((scale + (mScaleFactor * 2)) <= maxZoom) {
                return scale + mScaleFactor;
            } else {
                mDoubleTapDirection = -1;
                return maxZoom;
            }
        } else {
            mDoubleTapDirection = 1;
            return 1f;
        }
    }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
        if (!mScrollEnabled)
            return false;

        if (e1 == null || e2 == null)
            return false;
        if (e1.getPointerCount() > 1 || e2.getPointerCount() > 1)
            return false;
        //缩放手势进行中
        if (mScaleDetector.isInProgress())
            return false;
        if (getScale() == 1f)
            return false;

        mUserScaled = true;
        // scrollBy(distanceX, distanceY);
        scrollBy(-distanceX, -distanceY);
        // RectF r = getBitmapRect();
        // System.out.println(r.left + "   " + r.top + "   " + r.right + "   "
        // + r.bottom);
        invalidate();
        return true;
    }

    /**
     * @param e1
     * @param e2
     * @param velocityX
     * @param velocityY
     * @return
     */
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        if (!mScrollEnabled)
            return false;

        if (mFlingListener != null) {
            mFlingListener.onFling(e1, e2, velocityX, velocityY);
        }

        if (e1.getPointerCount() > 1 || e2.getPointerCount() > 1)
            return false;
        if (mScaleDetector.isInProgress())
            return false;
        if (getScale() == 1f)
            return false;

        float diffX = e2.getX() - e1.getX();
        float diffY = e2.getY() - e1.getY();
        if (Math.abs(velocityX) > 800 || Math.abs(velocityY) > 800) {
            mUserScaled = true;
            // System.out.println("on fling scroll");
            scrollBy(diffX / 2, diffY / 2, 300);
            invalidate();
            return true;
        }

        return false;
    }

    /**
     * Determines whether this ImageViewTouch can be scrolled.
     *
     * @param direction - positive direction value means scroll from right to left,
     *                  negative value means scroll from left to right
     * @return true if there is some more place to scroll, false - otherwise.
     */
    public boolean canScroll(int direction) {
        RectF bitmapRect = getBitmapRect();
        updateRect(bitmapRect, mScrollRect);
        Rect imageViewRect = new Rect();
        getGlobalVisibleRect(imageViewRect);

        if (null == bitmapRect) {
            return false;
        }

        if (bitmapRect.right >= imageViewRect.right) {
            if (direction < 0) {
                return Math.abs(bitmapRect.right - imageViewRect.right) > SCROLL_DELTA_THRESHOLD;
            }
        }

        double bitmapScrollRectDelta = Math.abs(bitmapRect.left
                - mScrollRect.left);
        return bitmapScrollRectDelta > SCROLL_DELTA_THRESHOLD;
    }

    /**
     * @author
     */
    public class GestureListener extends
            GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {

            if (null != mSingleTapListener) {
                mSingleTapListener.onSingleTapConfirmed();
            }

            return super.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i(LOG_TAG, "onDoubleTap. double tap enabled? "
                    + mDoubleTapEnabled);
            if (mDoubleTapEnabled) {
                mUserScaled = true;
                float scale = getScale();
                float targetScale = scale;
                targetScale = onDoubleTapPost(scale, getMaxScale());
                targetScale = Math.min(getMaxScale(),
                        Math.max(targetScale, getMinScale()));
                zoomTo(targetScale, e.getX(), e.getY(),
                        DEFAULT_ANIMATION_DURATION);
                invalidate();
            }

            if (null != mDoubleTapListener) {
                mDoubleTapListener.onDoubleTap();
            }

            return super.onDoubleTap(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            if (isLongClickable()) {
                if (!mScaleDetector.isInProgress()) {
                    setPressed(true);
                    performLongClick();
                }
            }
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            return ImageViewTouch.this.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                               float velocityY) {
            return ImageViewTouch.this.onFling(e1, e2, velocityX, velocityY);
        }
    }// end inner class

    public class ScaleListener extends
            ScaleGestureDetector.SimpleOnScaleGestureListener {
        protected boolean mScaled = false;

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float span = detector.getCurrentSpan() - detector.getPreviousSpan();
            float targetScale = getScale() * detector.getScaleFactor();
            // System.out.println("span--->" + span);
            if (mScaleEnabled) {
                if (mScaled && span != 0) {
                    mUserScaled = true;
                    targetScale = Math.min(getMaxScale(),
                            Math.max(targetScale, getMinScale() - 0.1f));
                    zoomTo(targetScale, detector.getFocusX(),
                            detector.getFocusY());
                    mDoubleTapDirection = 1;
                    invalidate();
                    return true;
                }

                // This is to prevent a glitch the first time
                // image is scaled.
                if (!mScaled)
                    mScaled = true;
            }
            return true;
        }
    }// end inner class

    public void resetImage() {
        float scale = getScale();
        float targetScale = scale;
        targetScale = Math.min(getMaxScale(),
                Math.max(targetScale, getMinScale()));
        zoomTo(targetScale, 0, 0, DEFAULT_ANIMATION_DURATION);
        invalidate();
    }

    public interface OnImageViewTouchDoubleTapListener {

        void onDoubleTap();
    }

    public interface OnImageViewTouchSingleTapListener {

        void onSingleTapConfirmed();
    }

    public interface OnImageFlingListener {
        void onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
    }

   /* *//**
     * 用于旋转缩放的Bitmap
     *//*
    private Bitmap mBitmap;
    *//*** 图片的最大缩放比例*//*
    public static final float MAX_SCALE = 10.0f;
    *//*** 图片的最小缩放比例*//*
    public static final float MIN_SCALE = 0.3f;
    *//*** 初始状态*//*
    public static final int STATUS_INIT = 0;
    *//*** 拖动状态*//*
    public static final int STATUS_DRAG = 1;
    *//*** 旋转或者放大状态*//*
    public static final int STATUS_ROTATE_ZOOM = 2;
    *//*** 缩放，旋转图标的宽和高*//*
    private int mDrawableWidth, mDrawableHeight;
    *//*** 用于缩放，旋转的图标*//*
    private Drawable controlDrawable;
    *//*** 用于缩放，旋转的控制点的坐标*//*
    private Point mControlPoint = new Point();
    private PointF mPreMovePointF = new PointF();
    private PointF mCurMovePointF = new PointF();
    *//*** SingleTouchView的中心点坐标，相对于其父类布局而言的*//*
    private PointF mCenterPoint = new PointF();
    *//*** SingleTouchView距离父类布局的左间距*//*
    private int mViewPaddingLeft;
    *//*** SingleTouchView距离父类布局的上间距*//*
    private int mViewPaddingTop;
    *//*** 图片的旋转角度*//*
    private float mDegree = DEFAULT_DEGREE;
    *//*** 图片的缩放比例*//*
    private float mScale = DEFAULT_SCALE;
    *//*** 用于缩放，旋转，平移的矩阵*//*
    private Matrix matrix = new Matrix();
    *//*** 外边框与图片之间的间距, 单位是dip*//*
    private int framePadding = DEFAULT_FRAME_PADDING;
    *//*** 外边框颜色*//*
    private int frameColor = DEFAULT_FRAME_COLOR;
    *//*** 外边框线条粗细, 单位是 dip*//*
    private int frameWidth = DEFAULT_FRAME_WIDTH;
    *//*** 是否处于可以缩放，平移，旋转状态*//*
    private boolean isEditable = DEFAULT_EDITABLE;
    *//*** View的宽度和高度，随着图片的旋转而变化(不包括控制旋转，缩放图片的宽高)*//*
    private int mViewWidth, mViewHeight;

    *//*** 控制缩放，旋转图标所在四个点得位置*//*
    public static final int LEFT_TOP = 0;
    public static final int RIGHT_TOP = 1;
    public static final int RIGHT_BOTTOM = 2;
    public static final int LEFT_BOTTOM = 3;
    *//*** 图片四个点坐标*//*
    private Point mLTPoint;
    private Point mRTPoint;
    private Point mRBPoint;
    private Point mLBPoint;

    *//*** 图片在旋转时x方向的偏移量*//*
    private int offsetX;
    *//*** 图片在旋转时y方向的偏移量*//*
    private int offsetY;

    *//*** 当前所处的状态*//*
    private int mStatus = STATUS_INIT;
    public static final int DEFAULT_FRAME_PADDING = 8;
    public static final float DEFAULT_SCALE = 1.5f;
    public static final float DEFAULT_DEGREE = 0;
    public static final int DEFAULT_CONTROL_LOCATION = RIGHT_BOTTOM;
    public static final boolean DEFAULT_EDITABLE = true;
    public static final int DEFAULT_FRAME_COLOR = Color.WHITE;
    public static final int DEFAULT_FRAME_WIDTH = 2;
    *//*** 画外围框的Path*//*
    private Path mPath = new Path();

    *//*** 画外围框的画笔*//*
    private Paint mPaint ;
    *//*** 控制图标所在的位置（比如左上，右上，左下，右下）*//*
    private int controlLocation = DEFAULT_CONTROL_LOCATION;

    *//**
     * 设置旋转图
     * @param bitmap
     *//*
    public void setImageBitmap(Bitmap bitmap){
        this.mBitmap = bitmap;
        if(bitmap != null){
            transformDraw();
        }

    }
    *//**
     * Clear the current drawable
     *//*
    public void clear() {
        setImageBitmap(null);
    }

    *//**
     * 设置Matrix, 强制刷新
     *//*
    private void transformDraw(){
        int bitmapWidth = (int)(mBitmap.getWidth() * mScale);
        int bitmapHeight = (int)(mBitmap.getHeight()* mScale);
        computeRect(-framePadding, -framePadding, bitmapWidth + framePadding, bitmapHeight + framePadding, mDegree);

        //设置缩放比例
        matrix.setScale(mScale, mScale);
        //绕着图片中心进行旋转
        matrix.postRotate(mDegree % 360, bitmapWidth/2, bitmapHeight/2);
        //设置画该图片的起始点
        matrix.postTranslate(offsetX + mDrawableWidth/2, offsetY + mDrawableHeight/2);

        invalidate();
    }
    *//*@Override
    protected void onDraw(Canvas canvas) {
        //每次draw之前调整View的位置和大小
        adjustLayout();

        super.onDraw(canvas);

        if(mBitmap == null) return;
        canvas.drawBitmap(mBitmap, matrix, null);

        //处于可编辑状态才画边框和控制图标
        if(isEditable){
            mPath.reset();
            mPath.moveTo(mLTPoint.x, mLTPoint.y);
            mPath.lineTo(mRTPoint.x, mRTPoint.y);
            mPath.lineTo(mRBPoint.x, mRBPoint.y);
            mPath.lineTo(mLBPoint.x, mLBPoint.y);
            mPath.lineTo(mLTPoint.x, mLTPoint.y);
            mPath.lineTo(mRTPoint.x, mRTPoint.y);
            canvas.drawPath(mPath, mPaint);
            //画旋转, 缩放图标

            controlDrawable.setBounds(mControlPoint.x - mDrawableWidth / 2,
                    mControlPoint.y - mDrawableHeight / 2, mControlPoint.x + mDrawableWidth
                            / 2, mControlPoint.y + mDrawableHeight / 2);
            controlDrawable.draw(canvas);
        }
    }*//*

    *//**
     * 获取四个点和View的大小
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @param degree
     *//*
    private void computeRect(int left, int top, int right, int bottom, float degree){
        Point lt = new Point(left, top);
        Point rt = new Point(right, top);
        Point rb = new Point(right, bottom);
        Point lb = new Point(left, bottom);
        Point cp = new Point((left + right) / 2, (top + bottom) / 2);
        mLTPoint = obtainRoationPoint(cp, lt, degree);
        mRTPoint = obtainRoationPoint(cp, rt, degree);
        mRBPoint = obtainRoationPoint(cp, rb, degree);
        mLBPoint = obtainRoationPoint(cp, lb, degree);

        //计算X坐标最大的值和最小的值
        int maxCoordinateX = getMaxValue(mLTPoint.x, mRTPoint.x, mRBPoint.x, mLBPoint.x);
        int minCoordinateX = getMinValue(mLTPoint.x, mRTPoint.x, mRBPoint.x, mLBPoint.x);;

        mViewWidth = maxCoordinateX - minCoordinateX ;


        //计算Y坐标最大的值和最小的值
        int maxCoordinateY = getMaxValue(mLTPoint.y, mRTPoint.y, mRBPoint.y, mLBPoint.y);
        int minCoordinateY = getMinValue(mLTPoint.y, mRTPoint.y, mRBPoint.y, mLBPoint.y);

        mViewHeight = maxCoordinateY - minCoordinateY ;


        //View中心点的坐标
        Point viewCenterPoint = new Point((maxCoordinateX + minCoordinateX) / 2, (maxCoordinateY + minCoordinateY) / 2);

        offsetX = mViewWidth / 2 - viewCenterPoint.x;
        offsetY = mViewHeight / 2 - viewCenterPoint.y;



        int halfDrawableWidth = mDrawableWidth / 2;
        int halfDrawableHeight = mDrawableHeight /2;

        //将Bitmap的四个点的X的坐标移动offsetX + halfDrawableWidth
        mLTPoint.x += (offsetX + halfDrawableWidth);
        mRTPoint.x += (offsetX + halfDrawableWidth);
        mRBPoint.x += (offsetX + halfDrawableWidth);
        mLBPoint.x += (offsetX + halfDrawableWidth);

        //将Bitmap的四个点的Y坐标移动offsetY + halfDrawableHeight
        mLTPoint.y += (offsetY + halfDrawableHeight);
        mRTPoint.y += (offsetY + halfDrawableHeight);
        mRBPoint.y += (offsetY + halfDrawableHeight);
        mLBPoint.y += (offsetY + halfDrawableHeight);

        mControlPoint = LocationToPoint(controlLocation);
    }


    *//**
     * 根据位置判断控制图标处于那个点
     * @return
     *//*
    private Point LocationToPoint(int location){
        switch(location){
            case LEFT_TOP:
                return mLTPoint;
            case RIGHT_TOP:
                return mRTPoint;
            case RIGHT_BOTTOM:
                return mRBPoint;
            case LEFT_BOTTOM:
                return mLBPoint;
        }
        return mLTPoint;
    }
    *//**
     * 两个点之间的距离
     * @return
     *//*
    private float distance4PointF(PointF pf1, PointF pf2) {
        float disX = pf2.x - pf1.x;
        float disY = pf2.y - pf1.y;
        return (float) Math.sqrt(disX * disX + disY * disY);
    }

    *//**
     * 获取变长参数最大的值
     * @param array
     * @return
     *//*
    public int getMaxValue(Integer...array){
        List<Integer> list = Arrays.asList(array);
        Collections.sort(list);
        return list.get(list.size() -1);
    }


    *//**
     * 获取变长参数最大的值
     * @param array
     * @return
     *//*
    public int getMinValue(Integer...array){
        List<Integer> list = Arrays.asList(array);
        Collections.sort(list);
        return list.get(0);
    }

    *//**
     * 获取旋转某个角度之后的点
     * @param source
     * @param degree
     * @return
     *//*
    public static Point obtainRoationPoint(Point center, Point source, float degree) {
        //两者之间的距离
        Point disPoint = new Point();
        disPoint.x = source.x - center.x;
        disPoint.y = source.y - center.y;

        //没旋转之前的弧度
        double originRadian = 0;

        //没旋转之前的角度
        double originDegree = 0;

        //旋转之后的角度
        double resultDegree = 0;

        //旋转之后的弧度
        double resultRadian = 0;

        //经过旋转之后点的坐标
        Point resultPoint = new Point();

        double distance = Math.sqrt(disPoint.x * disPoint.x + disPoint.y * disPoint.y);
        if (disPoint.x == 0 && disPoint.y == 0) {
            return center;
            // 第一象限
        } else if (disPoint.x >= 0 && disPoint.y >= 0) {
            // 计算与x正方向的夹角
            originRadian = Math.asin(disPoint.y / distance);

            // 第二象限
        } else if (disPoint.x < 0 && disPoint.y >= 0) {
            // 计算与x正方向的夹角
            originRadian = Math.asin(Math.abs(disPoint.x) / distance);
            originRadian = originRadian + Math.PI / 2;

            // 第三象限
        } else if (disPoint.x < 0 && disPoint.y < 0) {
            // 计算与x正方向的夹角
            originRadian = Math.asin(Math.abs(disPoint.y) / distance);
            originRadian = originRadian + Math.PI;
        } else if (disPoint.x >= 0 && disPoint.y < 0) {
            // 计算与x正方向的夹角
            originRadian = Math.asin(disPoint.x / distance);
            originRadian = originRadian + Math.PI * 3 / 2;
        }

        // 弧度换算成角度
        originDegree = radianToDegree(originRadian);
        resultDegree = originDegree + degree;

        // 角度转弧度
        resultRadian = degreeToRadian(resultDegree);

        resultPoint.x = (int) Math.round(distance * Math.cos(resultRadian));
        resultPoint.y = (int) Math.round(distance * Math.sin(resultRadian));
        resultPoint.x += center.x;
        resultPoint.y += center.y;

        return resultPoint;
    }

    *//*** 弧度换算成角度** @return*//*
    public static double radianToDegree(double radian) {
        return radian * 180 / Math.PI;
    }
    *//**
     * 角度换算成弧度
     * @param degree
     * @return
     *//*
    public static double degreeToRadian(double degree) {
        return degree * Math.PI / 180;
    }

    *//**
     * 根据点击的位置判断是否点中控制旋转，缩放的图片， 初略的计算
     * @param x
     * @param y
     * @return
     *//*
    private int JudgeStatus(float x, float y){
        PointF touchPoint = new PointF(x, y);
        PointF controlPointF = new PointF(mControlPoint);

        //点击的点到控制旋转，缩放点的距离
        float distanceToControl = distance4PointF(touchPoint, controlPointF);

        //如果两者之间的距离小于 控制图标的宽度，高度的最小值，则认为点中了控制图标
        if(distanceToControl < Math.min(mDrawableWidth/2, mDrawableHeight/2)){
            return STATUS_ROTATE_ZOOM;
        }

        return STATUS_DRAG;

    }
    *//**
     * 调整View的大小，位置
     *//*
    private void adjustLayout(){
        int actualWidth = mViewWidth + mDrawableWidth;
        int actualHeight = mViewHeight + mDrawableHeight;

        int newPaddingLeft = (int) (mCenterPoint.x - actualWidth /2);
        int newPaddingTop = (int) (mCenterPoint.y - actualHeight/2);

        if(mViewPaddingLeft != newPaddingLeft || mViewPaddingTop != newPaddingTop){
            mViewPaddingLeft = newPaddingLeft;
            mViewPaddingTop = newPaddingTop;
            if (mStatus != STATUS_INIT){
                layout(newPaddingLeft, newPaddingTop, newPaddingLeft + actualWidth, newPaddingTop + actualHeight);
            }
        }
    }
*/
}
